/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int maxDepth(struct TreeNode* root) {
    if (root == NULL)
    {
        return 0;
    }


    int lh = maxDepth(root->left);
    int rh = maxDepth(root->right);

    return lh > rh ? lh + 1 : rh + 1;
}


bool isBalanced(struct TreeNode* root) {
    if (root == NULL)
    {
        return true;
    }

    int lh = maxDepth(root->left);
    int rh = maxDepth(root->right);
    if (abs(lh - rh) <= 1)
    {
        return isBalanced(root->left) &&
            isBalanced(root->right);
    }
    else
    {
        return false;
    }

    return true;
}